package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.common.pojo.BlackList;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.server.vo.BlackListVo;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MyCenterService {
    @Autowired
    private BlackListService blackListService;
    @Autowired
    private UserInfoService userInfoService;

    // 分页查询黑名单列表
    public PageResult queryBlackList(Integer page, Integer pageSize) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);

        // 校验token
        User user = UserThreadLocal.get();

        IPage<BlackList> queryBackList = blackListService.queryBlackList(user.getId(), page, pageSize);

        List<BlackList> records = queryBackList.getRecords();
        if (CollUtil.isEmpty(records)){
            return pageResult;
        }

        // 拿到黑名单用户的Id
        List<Object> blackuserids = CollUtil.getFieldValues(records, "blackuserid");
        List<UserInfo> userInfoList = userInfoService.queryUserInfoByUserIdList(blackuserids);

        ArrayList<BlackListVo> result = new ArrayList<>();
        BlackListVo blackListVo;
        for (UserInfo userInfo : userInfoList) {
            blackListVo = new BlackListVo();
            blackListVo.setNickname(userInfo.getNickName());
            blackListVo.setAvatar(userInfo.getLogo());
            blackListVo.setGender(userInfo.getSex().getValue() == 1 ? "man":"woman");
            blackListVo.setAge(userInfo.getAge());
            blackListVo.setId(userInfo.getUserId());

            result.add(blackListVo);
        }
        pageResult.setItems(result);
        return pageResult;
    }

    // 移除黑名单
    public void delBlackList(Long userId) {
        User user = UserThreadLocal.get();
        blackListService.delBlackList(user.getId(),userId);
    }
}
